#pragma once

#include "JobQueue.h"
#include <mysql/mysql.h>

struct thread_pool;

typedef struct thread {
    int id;             // 对人类友好的id
    pthread_t tid;
    MYSQL* mysql;               // MySQL连接
    struct thread_pool* tpool;  // 指向线程池
} Thread;

typedef struct thread_pool {
    Thread* threads;  // 保存线程的id
    JobQueue jobs;       // 任务队列
    volatile int threads_alive; // 每次从内存中读取变量的值，而不会使用寄存器中值
    volatile int threads_working;
    volatile int keep_alive;    // 线程池中的线程是否继续存活
    pthread_mutex_t count_lock;
    pthread_cond_t  all_idle;   // 指示：是不是所有线程都是空闲
} ThreadPool;

void threadpool_init(ThreadPool* tpool, int n);
void threadpool_add_task(ThreadPool* tpool, void (*job)(void*, struct thread*), void* arg);
// 等待所有线程都处于空闲状态
void threadpool_wait(ThreadPool* tpool);
void threadpool_destroy(ThreadPool* tpool);
